<?php
namespace Merchant\Controller;

class IndexController extends AdminController {
    
    /**
     * @todo    商户平台页面
     */
    public function index(){
        //获取活动列表
        //$activity_array = D('Admin/ServiceApi')->getServiceActivity('1,2,3,4');
        $activity_array = D('Admin/Activity','Service')->getActivityList(0);//活动列表
        
    	$this->meta_title= '家家户户商户平台';
    	$this->assign('activity_array', $activity_array);
    	$this->display();
    }
    
    /**
     * @todo    获取店铺列表
     * @param   int     activity_id     活动ID
     */
    public function getShopList(){

        $activity_id = (int)I('activity_id');
        //获取店铺API
        $shop_array = D('Api/Merchant','Service')->getMerchantShopList($activity_id, UID);
        $result = array('status' => 1, 'info' => '');
        if($shop_array['status'] == 1){
            $html = '<option value="0" selected>选择店铺</option>';
            foreach ($shop_array['info'] as $key=>$val){
                $html .= '<option value="'.$val['shop_id'].'">'.$val['shop_name'].'</option>';
            }
            $result['info'] = $html;
        }
        $this->ajaxReturn($result);
    }
    
    /**
     * @todo    获取订单数据（列表）
     * @param   int     activity_id     活动ID
     * @param   int     shop_id         店铺ID
     * @param   string  paper_order     纸质订单号
     * @param   int     order_status    订单状态（未申请，处理中，已返款）
     * @param   int     page            当前页码
     */
    public function getList(){
        
        $activity_id = (int)I('activity_id');
        $shop_id = (int)I('shop_id');
        $paper_order = strip_tags(I('paper_order'));
        $order_status = (int)I('order_status');
        $page = (int)I('page');
        $start_time = (I('start_time'))? strtotime(I('start_time')) : 0 ;
        $end_time = (I('end_time'))? strtotime(I('end_time')) : NOW_TIME ;
        $apply_start_time = (I('apply_start_time'))? strtotime(I('apply_start_time')) : 0 ;
        $apply_end_time = (I('apply_end_time'))? strtotime(I('apply_end_time')) : NOW_TIME ;
        $back_start_time = (I('back_start_time'))? strtotime(I('back_start_time')) : 0 ;
        $back_end_time = (I('back_end_time'))? strtotime(I('back_end_time')) : NOW_TIME ;
        if($activity_id == 0){
            $this->error('请选择活动');
        }
        $map = array(
            'activity_id' => $activity_id,
            'merchant_id' => UID,
            'shop_id' => $shop_id,
            'paper_order' => $paper_order,
            'order_status' => $order_status,
            'page' => $page,
            'start_time' => $start_time,
            'end_time' => $end_time,
            'apply_start_time' => $apply_start_time,
            'apply_end_time' => $apply_end_time,
            'back_start_time' => $back_start_time,
            'back_end_time' => $back_end_time,
        );
        $result = D('Api/Merchant','Service')->getShopOrderList($map);
        if($result['status'] == 1){
            $count = $result['info']['count'];
            if($count > 10){
                $_page = $this->getPage($result['info']['count'], $page);
                $result['info']['page'] = $_page;
            }else{
                $result['info']['page'] = '';
            }          
        }
        $this->ajaxReturn($result);
    }
    
    /**
     * @todo    申请核销
     * @param   int     order_id    主订单的ID
     * @param   int     number      识别码（校验码）
     */
    public function apply(){
        
        $order_id = (int)I('order_id');
        $number = (int)I('number');
        //申请API
        $result = D('Api/Merchant','Service')->applyAccount($order_id, UID, $number);
        $this->ajaxReturn($result);
    }
    
    /**
     * @todo    分页page
     * @param   int     count   总数
     * @param   int     page    当前页码
     */
    public function getPage($count, $page){
        $num = $count/50;
        $p = ($num == (int)$num)? (int)$num - 1 : (int)$num ;
        //分页展示数量
        $i = 0;
        $end = $p;
        if($p > 9){
            $i = ($page > 4)? $page - 4 : 0 ;
            $end = (($page + 4) > $num)? $num : $page + 4 ;
        }
        $_page = '<nav style="float:right;"><ul class="pagination"><li class="active"><a href="javascript:void(0);">共 '.$count.' 条</a></li><li>
            <a href="javascript:jump(0)" aria-label="Previous"><span aria-hidden="true">第一页</span></a></li>';
        for ($i;$i<=$end;$i++){
            if($page == $i){
                $_page .= '<li class="active"><a href="javascript:void(0);">'.($i + 1).'</a></li>';
            }else{
                $_page .= '<li><a href="javascript:jump('.$i.')">'.($i + 1).'</a></li>';
            }
        }
        $_page .= '<li><a href="javascript:jump('.$p.')" aria-label="Next"><span aria-hidden="true">最后一页</span></a></li><li class="active"><a href="javascript:void(0);">共 '.($p + 1).' 页</a></li></ul></nav>';
        return $_page;
    }
    
}